<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>计算数组的差集</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-diff-ukey.html">« array_diff_ukey</a></li>
      <li style="float: right;"><a href="function.array-fill-keys.html">array_fill_keys »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>计算数组的差集</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-diff" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_diff</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_diff</span> &mdash; <span class="dc-title">计算数组的差集</span></p>

   </div> 

 <div class="refsect1 description" id="refsect1-function.array-diff-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_diff</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">...$arrays</code></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   对比 <code class="parameter">array</code> 和其他一个或者多个数组，返回在
     <code class="parameter">array</code> 中但是不在其他 array 里的值。
  </p>
 </div>

    <div class="refsect1 parameters" id="refsect1-function.array-diff-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       要被对比的数组
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">arrays</code></dt>

     <dd>

      <p class="para">
       和更多数组进行比较
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

   
    <div class="refsect1 returnvalues" id="refsect1-function.array-diff-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    返回一个数组，该数组包括了所有在
     <code class="parameter">array</code> 中但是不在任何其它参数数组中的值。注意键名保留不变。
    保留数组 <code class="parameter">array</code> 里的键。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-diff-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
  <td>8.0.0</td>
  <td>
  现在可以仅使用一个参数调用此函数。以前，至少需要两个参数。
  </td>
</tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-diff-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4906">
    <p><strong>示例 #1 <span class="function"><strong>array_diff()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     在 <var class="varname">$array1</var> 中多次出现的值一样处理，输出结果为：
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [1] =&gt; blue
)
</pre></div>
    </div>
   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-4907">
    <p><strong>示例 #2 <span class="function"><strong>array_diff()</strong></span> 不匹配类型的示例</strong></p>
    <div class="example-contents"><p>
     两个元素只有在 <code class="literal">(string) $elem1 === (string) $elem2</code> 相等时视为一致。 也就是<a href="language.types.string.html#language.types.string.casting" class="link">字符串转换表达</a>相同。
    </p></div>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;数组无法转换成字符串时会产生&nbsp;Notice&nbsp;警告<br /></span><span style="color: #0000BB">$source&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$filter&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;[</span><span style="color: #0000BB">5</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$source</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$filter</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;而这个就可以，因为对象可以转换成字符串<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">S&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$v</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">string&nbsp;$v</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">v&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$v</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__toString</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">v</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$source&nbsp;</span><span style="color: #007700">=&nbsp;[new&nbsp;</span><span style="color: #0000BB">S</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">),&nbsp;new&nbsp;</span><span style="color: #0000BB">S</span><span style="color: #007700">(</span><span style="color: #DD0000">'b'</span><span style="color: #007700">),&nbsp;new&nbsp;</span><span style="color: #0000BB">S</span><span style="color: #007700">(</span><span style="color: #DD0000">'c'</span><span style="color: #007700">)];<br /></span><span style="color: #0000BB">$filter&nbsp;</span><span style="color: #007700">=&nbsp;[new&nbsp;</span><span style="color: #0000BB">S</span><span style="color: #007700">(</span><span style="color: #DD0000">'b'</span><span style="color: #007700">),&nbsp;new&nbsp;</span><span style="color: #0000BB">S</span><span style="color: #007700">(</span><span style="color: #DD0000">'c'</span><span style="color: #007700">),&nbsp;new&nbsp;</span><span style="color: #0000BB">S</span><span style="color: #007700">(</span><span style="color: #DD0000">'d'</span><span style="color: #007700">)];<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_diff</span><span style="color: #007700">(</span><span style="color: #0000BB">$source</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$filter</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;$result&nbsp;包含了一个&nbsp;S('a');&nbsp;实例<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     想要使用函数来比较，可使用 <span class="function"><a href="function.array-udiff.html" class="function">array_udiff()</a></span>。
    </p></div>
   </div>
  </p>
 </div>

   
    <div class="refsect1 notes" id="refsect1-function.array-diff-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    注意本函数只检查了多维数组中的一维。当然可以用
      <code class="literal">array_diff($array1[0], $array2[0]);</code>
      检查更深的维度。
   </p>
  </p></blockquote>
 </div>

   
    <div class="refsect1 seealso" id="refsect1-function.array-diff-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-diff-assoc.html" class="function" rel="rdfs-seeAlso">array_diff_assoc()</a> - 带索引检查计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-udiff.html" class="function" rel="rdfs-seeAlso">array_udiff()</a> - 用回调函数比较数据来计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="function.array-intersect.html" class="function" rel="rdfs-seeAlso">array_intersect()</a> - 计算数组的交集</span></li>
    <li class="member"><span class="function"><a href="function.array-intersect-assoc.html" class="function" rel="rdfs-seeAlso">array_intersect_assoc()</a> - 带索引检查计算数组的交集</span></li>
   </ul>
  </p>
 </div>

   
  </div></div></div></body></html>